package com.example.woke1;

import java.util.ArrayList;
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.xl.xxl.imagecache.ImageLoader;

import com.http.HttpMethods;
import com.zhongcai.online.R;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.adapter.Zhouchouadter;
import com.woke.data.Data_zhongchou;
import com.woke.method.MyApp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class ZhongchouActivity extends Activity implements OnClickListener, OnItemClickListener {

	private ListView mListView;
	private ArrayList<Data_zhongchou> datalist = new ArrayList<Data_zhongchou>();
	private ImageLoader imageLoader;
	private LayoutInflater inflater;
	private Zhouchouadter adapter;
	private MyApp application;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zhongchou);
		
		application = (MyApp) getApplication();
		inflater = getLayoutInflater();
		intview();
		
	}
	
	@Override
	protected void onDestroy() {
		imageLoader.clearCache();
		imageLoader.clearMemoryCache();
		super.onDestroy();
	}

	private void intview() {
		imageLoader = new ImageLoader(this);
		findViewById(R.id.avzhongchou_image_back).setOnClickListener(this);
		findViewById(R.id.zhongchou_liner1).setOnClickListener(this);
		findViewById(R.id.zhongchou_liner2).setOnClickListener(this);
		findViewById(R.id.zhongchou_liner3).setOnClickListener(this);
		mListView = (ListView) findViewById(R.id.zhongzhou_listview);
		mListView.setOnItemClickListener(this);
		findViewById(R.id.zhongchou_text_more).setOnClickListener(this);
		adapter = new Zhouchouadter(inflater,datalist,imageLoader,application.screenWidth);
		mListView.setAdapter(adapter);
		getzhouchoulist();
	}
	
	private void getzhouchoulist() {
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "crowd");
		params.put("a", "hot");
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
//				Log.e("response", ""+response);
				try {
					String flag = response.getString("flag");
					String string = response.getString("info");
					if(flag.equals("success")){
						if(datalist.size() > 0){
							datalist.clear();
						}
						JSONObject object = response.getJSONObject("data");
						JSONArray array = object.getJSONArray("data");
						for (int i = 0; i < array.length(); i++) {
							JSONObject jsonObject = array.getJSONObject(i);
							int percent = jsonObject.getInt("percent");
							String count = jsonObject.getString("count");
							String status = jsonObject.getString("status");
							String product_name = jsonObject.getString("product_name");
							String true_money = jsonObject.getString("true_money");
							String product_img = jsonObject.getString("product_img");
							String type = jsonObject.getString("type");
							String id = jsonObject.getString("id");
							String product_content = jsonObject.getString("product_content");
							String end_time = jsonObject.getString("end_time");
							String money = jsonObject.getString("money");
							String create_time = jsonObject.getString("create_time");
							String is_hot = jsonObject.getString("is_hot");
							String start_time = jsonObject.getString("start_time");
							Data_zhongchou list = new Data_zhongchou(percent, count, status, product_name,
									true_money, product_img, type, id, product_content,
									end_time, money, create_time, is_hot, start_time);
							datalist.add(list);
						}
					}else{
						Toast.makeText(ZhongchouActivity.this, "获取数据失败！", Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				if(datalist.size() > 0){
					adapter.notifyDataSetChanged();
				}
				
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Log.e("response", "失败"+responseString);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
//				Log.e("response", "���");
			}
		});
	}


	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		switch (v.getId()) {
		case R.id.avzhongchou_image_back:
			finish();
			break;
			
		case R.id.zhongchou_liner1:
			intent.setClass(ZhongchouActivity.this, ZhongchoucActivity.class);
			intent.putExtra("title", "产品众筹");
			startActivity(intent);
			break;
			
		case R.id.zhongchou_liner2:
			intent.setClass(ZhongchouActivity.this, ZhongchoucActivity.class);
			intent.putExtra("title", "股权众筹");
			startActivity(intent);
			break;
			
		case R.id.zhongchou_liner3:
			intent.setClass(ZhongchouActivity.this, ZhongchoucActivity.class);
			intent.putExtra("title", "公益众筹");
			startActivity(intent);
			break;
			
		case R.id.zhongchou_text_more:
			getzhouchoulist();
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(ZhongchouActivity.this,ZhongchouinfoActivity.class);
		intent.putExtra("id", datalist.get(arg2).id);
		intent.putExtra("myzhong", "0");
		startActivity(intent);
	}
}
